using System;
using MonoTouch.Dialog;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using MonoTouch.ObjCRuntime;


namespace Socialite
{
	// 
	// A MonoTouch.Dialog.Element that renders a PlaceDetailsCell
	//
	[Register ("PlaceDetailsElement")]
	public class PlaceDetailsElement : Element , IElementSizing
	{
		static NSString key = new NSString ("placedetailselement");
		public Place Place;
		float _cellHeight;
		
		public PlaceDetailsElement (Place place) : base (null)
		{
			Place = place;	
		}
	
		// Gets a cell on demand, reusing cells
		public override UITableViewCell GetCell (UITableView tv)
		{
			var cell = tv.DequeueReusableCell (key) as PlaceDetailsCell;
			if (cell == null)
			{
				// We have to allocate a cell
				var views = NSBundle.MainBundle.LoadNib ("PlaceDetailsCell", tv, null);
				cell = Runtime.GetNSObject (views.ValueAt (0)) as PlaceDetailsCell;
			}
			
			cell.BindDataToCell (Place);
	
			return cell;
		}
	
		public override void Selected (DialogViewController dvc, UITableView tableView, NSIndexPath path)
		{
			//var placeDetails = new PlaceDetailsScreen (Place);
			//dvc.ActivateController (profile);
			//dvc.NavigationController.PushViewController (placeDetails, true);
		}
	
		public override bool Matches (string text)
		{
			return (Place.Name.IndexOf (text, StringComparison.CurrentCultureIgnoreCase) != -1);
		}
	
		public float GetHeight (UITableView tableView, NSIndexPath indexPath)
		{
			if (_cellHeight == 0) {
				var views = NSBundle.MainBundle.LoadNib ("PlaceDetailsCell", tableView, null);
				PlaceDetailsCell cell = Runtime.GetNSObject (views.ValueAt (0)) as PlaceDetailsCell;
					
				_cellHeight = cell.Bounds.Height;
			}
			
			return _cellHeight;
		}
	}
}

